package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.httprequest.MyHttpRequest;
import com.greenline.search.test.httprequest.PerformanceInfo;
import com.greenline.search.test.httprequest.ResponseInfo;
import com.greenline.search.test.httprequest.impl.MyHttpClientRequest;
import com.greenline.search.test.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-16
 * Time: 下午3:47
 * To change this template use File | Settings | File Templates.
 */
public class KeywordAnalysis {

    private Logger logger = LoggerFactory.getLogger(KeywordAnalysis.class);

    public static void main(String[] args) {
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/user_keyword.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath,"GBK");
        long startTime = System.currentTimeMillis();
        String baseUrl = "http://10.10.100.151:10000/doctor_search?fl=doctor_uuid&secondsort=0&rows=0&q=";
        MyHttpRequest httpRequest = new MyHttpClientRequest();
        System.out.println("request start...");
        List<ResponseInfo> responseInfoList = new ArrayList<ResponseInfo>();
        for(int i = 0; i < keywordList.size(); i++){
            if(i % 1000 == 0){
                System.out.println("Deal with " + i + " keywords already.");
            }
            ResponseInfo info = null;
            try {
                info = httpRequest.requestAndGetResponseInfo(baseUrl + URLEncoder.encode(keywordList.get(i), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            info.setKeyword(keywordList.get(i));
            responseInfoList.add(info);
        }

        System.out.println("request end ...");
        PerformanceInfo performanceInfo = new PerformanceInfo(responseInfoList);
        performanceInfo.printKeywordAndReturnCountLog();
        performanceInfo.printDetail();
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime/1000) + " seconds.");
    }

}
